package com.example.plant02

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import com.google.android.material.tabs.TabLayout
import java.util.*

class MainActivity : AppCompatActivity() {

    val TAG ="@@ljl"
    private var phoneNumber:Int?=null
    private var password:Int?=null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val number = findViewById<EditText>(R.id.phoneNumber)
        val pass = findViewById<EditText>(R.id.password)

        val phoneWatcher = object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG, "befer:$p0")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG, "on:$p0")
            }

            override fun afterTextChanged(p0: Editable?) {
                Log.i(TAG, "after:$p0")
            }
        }

        val passwordWatcher = object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG, "befer:$p0")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG, "on:$p0")
            }

            override fun afterTextChanged(p0: Editable?) {
                Log.i(TAG, "after:$p0")
            }
        }

        number.addTextChangedListener(phoneWatcher)
        pass.addTextChangedListener(passwordWatcher)
    }

    fun onClick(view:View){
        Log.d(TAG,"phone is $phoneNumber")
        Log.d(TAG,"password is $password")

        AlertDialog.Builder(this)
            .setTitle("请注意")
            .setMessage("确认登录吗？")
            .setNegativeButton("取消登录"){d,w->Toast.makeText(this,"已取消",Toast.LENGTH_LONG).show()}
            .setPositiveButton("登录看看"){d,w->toLogin()}.show()
    }

    private fun toLogin() {
        val process = findViewById<ProgressBar>(R.id.progress)
        process.visibility=View.VISIBLE
        process.progress=0

        var timer=Timer()
        val context= this

        val task = object : TimerTask() {
            override fun run() {
                Log.d(TAG,"progress = ${process.progress}")
                process.progress=process.progress+1
                if(process.progress==6)
                {

                    timer.cancel()
                    Handler(Looper.getMainLooper()).post{
                        process.visibility = View.GONE
                        startActivity(Intent(context,devide::class.java))
                    }
                }
            }
        }
        timer.schedule(task,0,500)
    }
}